package com.easy.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Order;
import com.easy.common.bean.OrderItem;
import com.easy.common.enumbean.OrderStatus;
import com.easy.common.service.IOrderService;
import com.easy.order.mapper.OrderItemMapper;
import com.easy.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderService implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public Order insert(Order order) {
        orderMapper.insert(order);
        return orderMapper.selectById(order.getId());
    }

    @Override
    public Order update(Order order) {
        orderMapper.updateById(order);
        return orderMapper.selectById(order.getId());
    }

    @Override
    public Order transact(Integer id) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("order_status", OrderStatus.COMPLETED)
                .set("deal_time", LocalDateTime.now())
                .eq("id", id);
        orderMapper.update(null, updateWrapper);
        return orderMapper.selectById(id);
    }

    @Override
    public Order cancel(Integer id) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("order_status", OrderStatus.CANCELLED)
                .eq("id", id);
        orderMapper.update(null, updateWrapper);
        return orderMapper.selectById(id);
    }

    @Override
    public Order getById(Integer id) {
        Order order = orderMapper.selectById(id);
        return order;
    }

    @Override
    public IPage<Order> getListByUserID(Page page, Integer userid) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userid);
        return orderMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<Order> getListByCustomerID(Page page, Integer customerid) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerid);
        return orderMapper.selectPage(page, queryWrapper);
    }

    @Override
    public OrderItem insertItem(OrderItem orderItem) {
        orderItemMapper.insert(orderItem);
        return orderItemMapper.selectById(orderItem.getId());
    }

    @Override
    public OrderItem updateItem(OrderItem orderItem) {
        orderItemMapper.updateById(orderItem);
        return orderItemMapper.selectById(orderItem.getId());
    }

    @Override
    public int deleteItem(Integer id) {
        return orderItemMapper.deleteById(id);
    }

    // 注意：原接口中没有这个方法，这里假设需要实现
    @Override
    public Order getOrderItemByID(Integer id) {
        // 1. 根据订单 ID 查询订单信息
        Order order = orderMapper.selectById(id);
        if (order != null) {
            // 2. 使用该订单 ID 查询对应的所有订单明细
            QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_id", id);
            List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
            // 3. 将订单明细设置到订单对象中
            order.setOrderItem(orderItems.get(0));
        }
        return order;
    }
}